package com.we823.project.common.file.in;

import java.io.File;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.stream.FileImageInputStream;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.hibernate.annotations.common.util.impl.LoggerFactory;
import org.slf4j.Logger;

import com.we823.project.common.file.ReadExcelUtil;
import com.we823.project.model.repo.MaterialStockModel;
import com.we823.utils.web.DateUtil;

public class ReadStockUtil extends ReadExcelUtil {
	private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(ReadStockUtil.class);
	
	public List<MaterialStockModel> getDataFromExcel(File excelFile,boolean hasHeader) throws Exception{
		HSSFWorkbook workbook = super.readExcel(excelFile);
		if (workbook!=null) {
			List<MaterialStockModel> materialStockModels = null;
			HSSFSheet sheet = workbook.getSheetAt(0);
			if (sheet!=null) {
				int rows = sheet.getPhysicalNumberOfRows();
				if (rows>0) {
					materialStockModels = new ArrayList<>();
					int startRow = hasHeader ? 1 : 0;
					for (int rowIndex = startRow; rowIndex < rows; rowIndex++) {
						int cells = sheet.getRow(rowIndex).getPhysicalNumberOfCells();
						if (cells==0) {
							throw new Exception("内容为空，无法转换");
						}else {
							MaterialStockModel materialStockModel = new MaterialStockModel();
							materialStockModel.setRepoId(1);
							materialStockModel.setAddtime(new java.util.Date());
							HSSFRow row = sheet.getRow(rowIndex);
							HSSFCell cell = row.getCell(0);
							
							String dateString = cell.getStringCellValue();
							if (!StringUtils.isEmpty(dateString)) {
								String[] datesStrings = StringUtils.split(dateString,".");
								if (datesStrings!=null && datesStrings.length>0 ) {
									try {
									int length = datesStrings.length;
									if (length==1) {
										int year = Integer.parseInt(datesStrings[0]);
										materialStockModel.setYear(year);
										int month = DateUtil.getMonth();
										int day = DateUtil.getDay();
										
										materialStockModel.setMonth(month);
										materialStockModel.setDay(day);
									}else if(length==2) {
										int year = Integer.parseInt(datesStrings[0]);
										int month = Integer.parseInt(datesStrings[1]);
										materialStockModel.setYear(Integer.parseInt(datesStrings[0]));
										materialStockModel.setMonth(Integer.parseInt(datesStrings[1]));
										if (year!=DateUtil.getYear() && month!=DateUtil.getMonth()) {
											int day = DateUtil.getMaxDay(year, month);
											materialStockModel.setDay(day);
										}
									}else {
										int year = Integer.parseInt(datesStrings[0]);
										int month = Integer.parseInt(datesStrings[1]);
										materialStockModel.setYear(Integer.parseInt(datesStrings[0]));
										materialStockModel.setMonth(Integer.parseInt(datesStrings[1]));
										if (year!=DateUtil.getYear() && month!=DateUtil.getMonth()) {
											int day = DateUtil.getMaxDay(year, month);
											materialStockModel.setDay(day);
										}
									}
									}catch(Exception exception) {
										throw new Exception("转换日期时发生错误，请检查后再继续");
									}
								}
								
							}
							
							//1.cell
							HSSFCell codeCell = row.getCell(1);
							materialStockModel.setMaterialCode(codeCell.getStringCellValue());
							
							//2. name
							HSSFCell nameCell = row.getCell(2);
							if(nameCell!=null) {
							   materialStockModel.setMaterialName(row.getCell(2).getStringCellValue());
							}else {
								LOGGER.error("name Cell is null");
							}
							
							//3.model
							HSSFCell modelCell = row.getCell(3);
							if (modelCell!=null) {
								materialStockModel.setModel(row.getCell(3).getStringCellValue());
							}else {
								LOGGER.error("model Cell is null");
							}
							
							
							//4.unit
							HSSFCell unitCell = row.getCell(4);
							if (unitCell!=null) {
								materialStockModel.setUnit(row.getCell(4).getStringCellValue());
							}else {
								LOGGER.error("unit Cell is null");
							}
							
							//6.stock
							HSSFCell stockCell = row.getCell(6);
							Float floatStock = 0f;
							try {
							double stock = stockCell.getNumericCellValue();
							floatStock = (float) stock;
							}catch(Exception ex) {
								floatStock = 0f;
							}
							
							materialStockModel.setStock(floatStock);
							materialStockModel.setLastStock(floatStock);
							
							materialStockModels.add(materialStockModel);
							
						}
					}
					
					
				}else {
					throw new Exception("内容为空，无法转换");
				}
				
				
			}
			return materialStockModels;
		}
		
		return null;
	}
}
